package com.alexb.tasksreport.resource;

import java.util.Collection;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import com.alexb.tasksreport.model.Person;
import com.alexb.tasksreport.model.Task;
import com.alexb.tasksreport.util.PMF;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@Path("/person/{id}")
public class PersonResource {

    @GET
    @Produces("application/json")
    public Person read(@PathParam("id") Long id) {
    	
    	PersistenceManager pm = PMF.get().getPersistenceManager();

    	Person person;
        try {
        	Key key = KeyFactory.createKey(Person.class.getSimpleName(), id);
        	person= pm.getObjectById(Person.class, key);
        } finally {
            pm.close();
        }
    	
        return person;
    }

    @POST
    @Produces("application/json")
    public Person update(@PathParam("id") Long id, @FormParam("firstName") String firstName, @FormParam("lastName") String lastName) {
    	
    	PersistenceManager pm = PMF.get().getPersistenceManager();

    	Person person;
        try {
        	Key key = KeyFactory.createKey(Person.class.getSimpleName(), id);
        	person= pm.getObjectById(Person.class, key);
        	person.setFirstName(firstName);
        	person.setLastName(lastName);
        } finally {
            pm.close();
        }
    	
        return person;
    }
    
    @DELETE
    @Produces("application/json")
    public Boolean delete(@PathParam("id") Long id) {
    	
    	PersistenceManager pm = PMF.get().getPersistenceManager();

    	Person person;
        try {
        	Key key = KeyFactory.createKey(Person.class.getSimpleName(), id);
        	person= pm.getObjectById(Person.class, key);
        	
        	Query query = pm.newQuery(Task.class);
        	query.setFilter("ownerID == idParam");
        	query.declareParameters("Long idParam");
        	Collection<Task> tasks =  (Collection<Task>) query.execute(id);
        	pm.deletePersistentAll(tasks);
        	
        	pm.deletePersistent(person);
        } finally {
            pm.close();
        }

        return Boolean.TRUE;
    }
    
    @Path("/tasks")
    @GET
    @Produces("application/json")
    public Collection<Task> ownedBy(@PathParam("id") Long id) {
    	
    	Collection<Task> tasks;
    	
    	PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
        	Query query = pm.newQuery(Task.class);
        	query.setFilter("ownerID == idParam");
        	query.declareParameters("Long idParam");
        	tasks = pm.detachCopyAll((Collection<Task>) query.execute(id));
        } finally {
            pm.close();
        }

        return tasks;
    }
}
